angular.module('revieworder', []).controller('revieworderCtrl', ['$scope', 'httpService', '$stateParams', 'alertmsg', 'confirmservice', '$interval', '$state', '$sce', function($scope, httpService, $stateParams, alertmsg, confirmservice, $interval, $state, $sce) {

    // $scope.handleLists = ['manager', 'tech', 'business', 'finance', 'cashier', 'edit_order', 'edit_amount'];
    $scope.changeOrderByRole = function(id, user) {
        if($scope.handleLists.all.indexOf(user.user_type) != -1) {
            $state.go('mine.changeorder', { id: id, isreview: 1 });
        } else {

        }
    };

    $scope.statesArrs = statesArrs;
    $scope.arrindex = 0;

    var id = $stateParams.id;
    // 根据id获取订单详情
    $scope.getOrderById = function() {
        httpService.query('web/orders/' + id).then(function(res) {
            if (res.code == 200) {
                $scope.order = res.order;
                updateIndex(res.order);
            }
        });
    };
    $scope.getOrderById();

    function updateIndex(order) {
        angular.forEach(statesArrs, function(v, k) {
            if (v.state.split(',').indexOf(order.workflow_state) != -1) {
                $scope.arrindex = k;
            }
        });
    }

    // 审核通过
    $scope.confirmOrder = function() {
        confirmservice('确认审核?').then(function(bool) {
            console.log('bool', bool)
            if (bool) {
                httpService.query('web/orders/' + id + '/confirm_upload', '', 'post').then(function(res) {
                    if (res.code == 200) {
                        $scope.order = res.order;
                        updateIndex(res.order);
                        alertmsg('审核通过');
                    } else {
                        alertmsg(res.msg);
                    }
                });
            }
        });
    };

    // 留言
    $scope.sendContent = function(id, content) {
        var first = false;
        if(content && !first) {
            first = true;
            httpService.query('web/orders/' + id + '/create_flow', { content: content }, 'post').then(function(res) {
                if(res.code == 200) {
                    $scope.order.flows.unshift(res.flow);
                    alertmsg('留言成功！');
                } else {
                    alertmsg(res.msg);
                }
                first = false;
            }, function() {
                first = false;
            });
        } else {
            alertmsg('留言不可以为空！');
        }
    };

    function showError(msg) {
        $scope.noperson = msg;
        $timeout(function() {
            $scope.noperson = '';
        }, 1500);
    }

    // 搜索安装师傅
    $scope.getMaster = function(keyword) {
        var first = 1;
        $scope.noperson = '';
        if (keyword && first == 1) {
            ++first;
            httpService.query('web/users/fetch_master', { keyword: keyword, page: 1, limit: limit }).then(function(res) {
                first == 1;
                if(res.code == 200) {
                    $scope.masters = res.users;
                } else {
                    alertmsg(res.msg);
                }
            }, function() {
                first == 1;
            });
        } else {
            first == 1
            showError('请输入关键字!');
        }
    };

    function waitGetMaster() {
        var timer;
        if(timer) {
            $interval.cancel(time);
        }

        timer = $interval(function() {
            $scope.getMaster($scope.keyword);
            $interval.cancel(timer);
        }, 500);
    }

    $scope.$watch('keyword', function(newData, oldData) {
        if(newData) {
            waitGetMaster();
        }
    });

    // 设定师傅
    $scope.setMaster = function(master) {
        oldorder = angular.copy($scope.order);
        $scope.order.facilitator_id = master.id;
        $scope.order.service_info.name = master.real_name;
        $scope.master = master;
        $scope.masters = null;
        $scope.keyword = '';
        $('#servermodal').modal('hide')
    };

    $scope.updateTimeAndMaster = function(order) {
        // $scope.setisinstalltime = true;
        var obj = {};
        obj.id = $scope.order.id;
        obj.facilitator_id = $scope.order.facilitator_id;
        // obj.install_date = $scope.order.install_date;
        // $scope.putOrderDetail(false, obj, true);
        httpService.query('web/orders/' + obj.id, obj, 'put').then(function(res) {
            console.log('res', res)
            if(res.code == 200) {
                alertmsg('订单成功指派给' + $scope.order.service_info.name + '了！');
                // window.history.go(-1);
            }
        })
    };

    $scope.downloadPdf = function(url) {
        var split_url = url.slice(-3);
        if(split_url == 'pdf') {
            window.open($sce.trustAsUrl(url), '_blank');
        }
    };

    $scope.corfirmInstall = function(id) {
        confirmservice('是否确认安装完成？').then(function(bool) {
            if (bool) {
                httpService.query('web/orders/' + id + '/state_event', {}, 'post').then(function(res) {
                    if (res.code == 200) {
                        $scope.order = res.order;
                        alertmsg('确认安装完成！');
                    } else {
                        alertmsg(res.msg);
                    }
                });
            }
        });
    };

    $scope.payOrder = function() {
        if (!isMobile) {
            pay_type = 'alipay_pc_direct';
        }
        httpService.query('web/orders/' + id + '/payment', { pay_type: pay_type }, 'post').then(function(res) {
            $('#payModal').modal('hide');
            if (res.code == 200) {
                $scope.order = res.order;
                updateIndex(res.order);
                alertmsg('支付成功!');
            } else {
                alertmsg(res.msg);
                if (res.code == 301) {
                    $state.go('mine.recharge', { money: $scope.order.total });
                }
            }
        });
    };

}]);
